<?php

class IndexController extends AppBaseC {

    //首页展示
    public function actionIndex() {
        $GoodModel = new AppGoodsModel();
        // 接收参数$num
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppHomeGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //下棋预告
    public function actionNs() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppNextSales($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //秋冬专题
    public function actionQd() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppWinterNewGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //天猫专场
    public function actionTm() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppTianMaoGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //淘宝
    public function actionTb() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getTBGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //京东页面展示
    public function actionJd() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppJdGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //九块九 页面展示
    public function actionJkj() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppJKJGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );

        echo CJSON::encode($return);
    }

    //今日特惠 页面展示
    public function actionHome() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getHomeGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //今日特惠按价格排序输出接口
    public function actionTodayPrice() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getHomeGoods($num, true, 'promo_price');
        $data = array();
        foreach ($goods['data'] as $rs) {
            $data[] = $rs;
        }
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $data,
        );
        echo CJSON::encode($return);
    }

    //今日特惠按价格排序输出接口
    public function actionTodaySales() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $order = "sales";
        $goods = $GoodModel->getHomeGoods($num, $cache = true, $order);
        $data = array();
        foreach ($goods['data'] as $op){
            $data[] = $op;
        }
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $data,
        );
        echo CJSON::encode($return);
    }

    //分类栏目下的商品列表
    public function actionCat() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppCatGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }
    //获取栏目列表
    public function actionCatlist(){
        $model = new AppGoodsModel();
        echo CJSON::encode($model->getCatlist());
    }

    /**
     * 获取积分兑换商品列表
     */
     public function actionExch() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getExchangeGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' =>$goods['data'],
        );       
        echo CJSON::encode($return);
        
    }
    /**
     * 用户下载
     * @return [type] [description]
     */
    public function actionDownload()
    {
        if(stripos($_SERVER['HTTP_USER_AGENT'],'iphone') === false){//安卓
            $this->redirect('/app/android/yzw.apk');
        }else{//iphone
//            header('Content-Type: text/html; charset=UTF-8');
//            exit('ios版本开发中,敬请期望');
            $this->redirect('https://itunes.apple.com/us/app/1zhe-wang/id914988082?l=zh&ls=1&mt=8');
        }       
    }


}
